package es.udc.emarketapp.emarket.model.purchase.purchaseFacade.actions;

import es.udc.emarketapp.emarket.model.order.vo.OrderVO;
import es.udc.emarketapp.emarket.model.orderDetails.dao.SQLOrderDetailsDAO;
import es.udc.emarketapp.emarket.model.orderDetails.dao.SQLOrderDetailsDAOFactory;
import es.udc.emarketapp.emarket.model.orderDetails.vo.OrderDetailsVO;
import es.udc.emarketapp.emarket.model.orderLine.dao.SQLOrderLineDAO;
import es.udc.emarketapp.emarket.model.orderLine.vo.OrderLineVO;
import es.udc.emarketapp.emarket.model.orderLine.dao.SQLOrderLineDAOFactory;

import java.sql.Connection;
import java.util.List;

import es.udc.fbellas.j2ee.util.exceptions.InternalErrorException;
import es.udc.fbellas.j2ee.util.exceptions.ModelException;
import es.udc.fbellas.j2ee.util.sql.NonTransactionalPlainAction;

public class FindOrderByIdAction implements NonTransactionalPlainAction {

	private Long idOrder;

	public FindOrderByIdAction(Long idOrder) {
		this.idOrder = idOrder;
	}

	public Object execute(Connection connection) throws InternalErrorException,
			ModelException {

		SQLOrderDetailsDAO orderDetailsDAO = SQLOrderDetailsDAOFactory.getDAO();	
		SQLOrderLineDAO orderLineDAO = SQLOrderLineDAOFactory.getDAO();

		OrderDetailsVO orderDetails =
                        orderDetailsDAO.findById(connection, idOrder);
		
		List<OrderLineVO> orderLines =
                        orderLineDAO.findByOrder(connection, idOrder,
                            orderDetails.getnCompra(), 1, 200);
		
		OrderVO order = new OrderVO(orderDetails, orderLines);
		
		return order;

	}
}
